﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06动态交互类DrawJig
{
    public class MoveJig:DrawJig
    {
        private List<Entity> jEntList;
        private Point3d jPointBase;
        private Point3d jPointPre;

        Matrix3d jMt = Matrix3d.Displacement(new Vector3d(0, 0, 0));
        public MoveJig(List<Entity> entList, Point3d pointBase)
        {
            jEntList = entList;
            jPointBase = pointBase;
            jPointPre = pointBase;
        }
        //重绘图形
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            for (int i = 0; i < jEntList.Count; i++)
            {
                draw.Geometry.Draw(jEntList[i]);
            }
            return true;
        }
        //获取鼠标在屏幕的运动,需要更新图形对象的属性
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
           // Database db = HostApplicationServices.WorkingDatabase;

           //声明一个提示类
            JigPromptPointOptions jppo = new JigPromptPointOptions("\n 指定第二个点或 <使用第一个点作为位移>:");
            jppo.Cursor = CursorType.RubberBand;
            jppo.BasePoint = jPointBase;
            jppo.UseBasePoint = true;
            jppo.Keywords.Add(" ");
            jppo.AppendKeywordsToMessage = false;
            jppo.UserInputControls = UserInputControls.Accept3dCoordinates;
            PromptPointResult ppr = prompts.AcquirePoint(jppo);
            Point3d curPoint = ppr.Value;
            //这对图形对象进行矩阵变换
            if (curPoint != jPointPre)
            {
                Vector3d vector = jPointPre.GetVectorTo(curPoint);
                jMt = Matrix3d.Displacement(vector);

                for (int i = 0; i < jEntList.Count; i++)
                {
                    jEntList[i].TransformBy(jMt);
                }
                //using (Transaction trans = db.TransactionManager.StartTransaction())
                //{
                //    for (int i = 0; i < jEntList.Count; i++)
                //    {
                //        Entity ent = (Entity)jEntList[i].ObjectId.GetObject(OpenMode.ForWrite);
                //        ent.TransformBy(jMt);
                //    }
                //    trans.Commit();
                //}
                
            }
           
            jPointPre = curPoint;
            
           
            return SamplerStatus.NoChange;
             
            
        }

        public List<Entity> GetEntityList()
        {
            return jEntList;
        }
    }
}
